/*
 * StockTableModel.java
 *
 * Created on December 11, 2007, 10:59 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.fie.models;

import com.fie.parser.Parser;
import com.fie.renderer.ColorRenderer;
import com.fie.renderer.LastRenderer;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import soap.StockQuote;

/**
 *
 * @author joseacuna-rohter
 */
public class StockTableModel extends AbstractTableModel{
    
    
    
    private Parser p;   
    private final Timer timer = new Timer();
    private boolean cont = true;
   // private static StockTableModel stm = new StockTableModel("bla");
    private String[] colNames={
        "Ticker","Last","% Change","Last Update","+/-"          
    };
    private Object[][] data ;
    private  ArrayList<String[]> ResultSets;
    private JTable stocksTable;
    /** Creates a new instance of StockTableModel */
    public StockTableModel(String ticker,JTable tb) {
                
        ResultSets=new ArrayList<String[]>();  
        stocksTable = tb;
        start();
                
    }
    
    
    public void start() {
        timer.schedule(new TimerTask() {
            public void run() {
                playSound();               
            }
            private void playSound() {
                
               updateRow();
               
               start();
            }
        }, 2000);
    }
    
    
    
    

    public void setValueAt(Object object, int i, int i0) {
        super.setValueAt(object, i, i0);
    }

    public Object getValueAt(int rowindex, int columnindex) {
       String[] row=ResultSets.get(rowindex);
       return row[columnindex];
    }

    public int getRowCount() {
         return ResultSets.size();
    }

    public int getColumnCount() {
        return colNames.length;
    }
    
    public String getColumnName(int param) {
        return colNames[param];
    }
    
    public void addRow(String ticker){
            
            p = new Parser(ticker);
        
            String[] row={
                ticker ,p.getLast(),p.getPercentChange(),p.getLastTime(),"="   
                
           };
        
        ResultSets.add(row);
        
    }
    
    
    public synchronized void removeRow(String ticker){
        Iterator it = ResultSets.iterator();
        String[] tmp;
        while(it.hasNext()){
          
                 tmp = ResultSets.get(ResultSets.indexOf(it.next()));
                 if(tmp[0].equals(ticker)){
                     ResultSets.remove(tmp);
                     break;
                 }                    
        }
        
    }
    
    public synchronized void updateRow(){
       
        
        TableColumnModel colModel = stocksTable.getColumnModel();
        TableColumn column = colModel.getColumn(2);
        TableColumn columnLast = colModel.getColumn(1);
        
        float pastTick;
        
        for(int i = 0; i<ResultSets.size();i++){
      
            String[] s = ResultSets.get(i);
            p = new Parser(s[0]);

            pastTick = Float.parseFloat(s[1]);
            
            //if(s[0].equals("CME"))
            //    pastTick = pastTick+1;
            //if(s[0].equals("MSFT"))
            //    pastTick = pastTick-1;
          
           
          
            if(pastTick - Float.parseFloat(p.getLast()) < 0)
                s[4] = "+";
            else if(pastTick - Float.parseFloat(p.getLast()) > 0)
                s[4] = "-";    
            else
                s[4] = "=";
            
            
            s[1] = p.getLast();   
            s[2] = p.getPercentChange();
            s[3] = p.getLastTime();

            cont = true;
            column.setCellRenderer(new ColorRenderer(2));
            columnLast.setCellRenderer(new LastRenderer(4));
      
            ResultSets.set(i,s);
            
            
        }
        
       
    }
    
}
